/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aliasMatching.cluster.eightCluster;

import aliasMatching.controller.TestFile;
import aliasMatching.controller.TimeClustering;
import aliasMatching.controller.TimeEquality;
import aliasMatching.model.Alias;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * clustering the user according to their passive time. the users are divided
 * into86 different cluster wrt time where they don't post anything in the
 * forum. After that the users clusters are created when they are mostly active
 * in discussion board.
 *
 * @author amendrashrestha
 */
public class EightUserClusterTest {

    static List users = new ArrayList();
    static List postTime = new ArrayList();
    static List nullcluster = new ArrayList();
    static List users0003 = new ArrayList();
    static List users0306 = new ArrayList();
    static List users0609 = new ArrayList();
    static List users0912 = new ArrayList();
    static List users1215 = new ArrayList();
    static List users1518 = new ArrayList();
    static List users1821 = new ArrayList();
    static List users2100 = new ArrayList();
    static List firstActivityPeak0003 = new ArrayList();
    static List firstActivityPeak0306 = new ArrayList();
    static List firstActivityPeak0609 = new ArrayList();
    static List firstActivityPeak0912 = new ArrayList();
    static List firstActivityPeak1215 = new ArrayList();
    static List firstActivityPeak1518 = new ArrayList();
    static List firstActivityPeak1821 = new ArrayList();
    static List firstActivityPeak2100 = new ArrayList();
    static HashMap<String, double[]> map = new HashMap<String, double[]>();
    static HashMap<List, List> activityPeakMap = new HashMap<List, List>();
    static HashMap<String, List> matchedUserMap;
    static HashMap<String, List> matrixMap = new HashMap<String, List>();
    static List firstUserPostTime, secondUserPostTime, firstUserPost, secondUserPost;
    static double[] timeStamp;
    static String firstUser, secondUser;
    static HashSet activeSuccessUser = new HashSet();
    //private static List userPostsTime;
    private static List<List> totalUserList;
    static HashMap<String, List> totalSleepingMap;
    //static HashMap<String, List> totalActivityMap;
    private static List<String> matchedUser;
    //private static List<List> firstPeakCluster;
    private static List<List> totalFirstActivityPeakCluster;
    static int success = 0;
    static int failure = 0;
    static int sleepingSuccess = 0;
    static int sleepingFailure = 0;
    static int activeSuccess = 0;
    static int activeFailure = 0;

    public EightUserClusterTest() {
    }

    public static void main(String args[]) {
        Alias tf = new Alias();


        try {
            users = tf.getAllUsers();
            splitUser(users);
            //System.out.println("Users:" + users);


//            users.add(216);
//            users.add(295);
//            users.add(98);
//            users.add(111);
//            users.add(194);

            //int randomUser = getRandomUser(users); [1172_A, 5856_A

//            List dividedUsers = divideTime(Integer.parseInt(users.get(0).toString()));
//            users.remove(0);
//            users.addAll(0, dividedUsers);

            //for(int i = 0; i < users.size(); i++){
            //String tempfirstUser = users.get(0).toString();
            //divideTime(tempfirstUser);
            //}


            System.out.println("Total Users: " + users.size());
//            System.out.println("Total User after Division: " + styloUser.size());

            System.out.println("Sleeping Success: " + sleepingSuccess);
            System.out.println("Sleeping Failure: " + sleepingFailure);
            System.out.println("");
            System.out.println("Active Success: " + activeSuccessUser.size());
            System.out.println("Active Failure: " + activeFailure);
            /*System.out.println("Active Success: " + activeSuccess);
             System.out.println("Active Failure: " + activeFailure);
             System.out.println("");
             System.out.println("Total Success: " + success);
             System.out.println("Total Failure: " + failure);
             *
             */



            //List tempList = returnMaxCluster(totalUserList);
            //int listSize = totalUserList.size();

            // System.out.println("Matched Users: " + matchedUser);
        } catch (SQLException ex) {
        }
    }

    /**
     * return one random user from bulk of users
     *
     * @param users
     * @return
     */
    private static int getRandomUser(List users) {
        int user;
        Collections.shuffle(users);
        user = Integer.parseInt(users.get(0).toString());
        return user;
    }

    /**
     * calculates the number of users where UserX_A and UserX_B go to the same
     * cluster
     *
     * @param ActivityPeakUsers
     * @param clusterSize
     */
    private static HashMap calculateMatchedList(List<List> ActivityPeakUsers, int clusterSize) {
        matchedUserMap = new HashMap<String, List>();

        for (int a = 0; a < ActivityPeakUsers.size(); a++) {
            List userA = new ArrayList();
            List userB = new ArrayList();
            List bUsers = ActivityPeakUsers.get(a);

            matchedUser = new ArrayList<String>();
            for (int b = 0; b < bUsers.size(); b++) {
                String tempUsers = bUsers.get(b).toString();

                if (tempUsers.contains("_")) {
                    String realUser = returnUser(tempUsers);
                    if (tempUsers.contains("_A")) {
                        userA.add(realUser);
                    } else {
                        userB.add(realUser);
                    }
                }

            } // end for
            if (userA.size() > 0 && userB.size() > 0) {
                List tempList = new ArrayList();
                //matchedUserMap.put(String.valueOf(a + 1), userA);
                tempList.add(userA.get(0).toString());

                for (int i = 0; i < bUsers.size(); i++) {
                    String tempUserA = bUsers.get(i).toString();

                    if (!tempUserA.contains("_")) {
                        tempList.add(tempUserA);
                    }
                }
                matchedUserMap.put(String.valueOf(a + 1), tempList);
                userA.clear();
                userB.clear();

                /*int matchedUserNumber = matchedUser.size();
                 double matchedPercentage = (matchedUserNumber * 100) / clusterSize;
                 System.out.println("\nMatched User Number: " + matchedUserNumber);
                 System.out.println("Matched Percentage : " + matchedPercentage);*/
            }
        }
        return matchedUserMap;
    }

    /**
     * return userID by removing _A or _B
     *
     * @param userID
     * @return
     */
    public static String returnUser(String userID) {
        String num = "";
        String regex = "(\\d+)";
        Matcher matcher = Pattern.compile(regex).matcher(userID);
        while (matcher.find()) {
            num = matcher.group();
            //System.out.println(num);
        }
        return num;
    }

    /**
     * the UserX is divided into UserX_A and UserX_B and respectively the post
     * time of UserX is also divided between UserX_A and UserX_B.
     *
     * @param user
     * @throws SQLException
     */
    public static void splitUser(List user) throws SQLException {

//         List firstUserList = new ArrayList();
//         firstUserList.add(user);

        //TimeStyloClusterEqualityTest testCluster = new TimeStyloClusterEqualityTest();
        // TimeClustering se = new TimeClustering(user);
        //  styloUser = se.aliases;
        int userSize = user.size();

        Alias[] tempAlias = new Alias[userSize + 1];
        Alias[] previousAlias = new Alias[1];
        //Alias[] tempAlias_B = new Alias[userSize];

        //String[] tempUser = new String[userSize + 1];

        for (int i = 0; i < 1000; i++) {
//            List activityPeakA = new ArrayList();
//            List activityPeakB = new ArrayList();
//            List activityPeak = new ArrayList();

            String tempUser = user.get(i).toString();
            dividePostTime(tempUser);
            System.out.println("Current User: " + tempUser);

            user.remove(i);

            tempAlias[i] = new Alias(firstUser);
            //tempAlias[i].setPostTime(firstUserPostTime);

            double[] tempTimeVectorA = Alias.getTimeVectorArray(firstUserPostTime);
            inactivityCluster(tempTimeVectorA, firstUser);
            /* double[] normalizedVectorA = returnNormalizedTimeVector(tempTimeVectorA);
             //boolean resultA = returnSleepingResult(normalizedVectorA);

             //return normalized time vector
             tempAlias[i].setTimeStamp(normalizedVectorA);

             //finding peak hour for userA
             int peakHourIndexA = peakHourIndex(normalizedVectorA);
             activityPeakA = activityPeak(peakHourIndexA, normalizedVectorA);
             activityPeakCluster(activityPeakA, firstUser);
             * */

            user.add(0, tempAlias[i]);


            //map.put(firstUser, normalizedVectorA);


            //for second user
            tempAlias[i + 1] = new Alias(secondUser);
            //tempAlias[i + 1].setPostTime(secondUserPostTime);

            double[] tempTimeVectorB = Alias.getTimeVectorArray(secondUserPostTime);
            inactivityCluster(tempTimeVectorB, secondUser);
            /*
             double[] normalizedVectorB = returnNormalizedTimeVector(tempTimeVectorB);
             tempAlias[i + 1].setTimeStamp(normalizedVectorB);
             int peakHourIndexB = peakHourIndex(normalizedVectorB);
             //System.out.println("Peak Hour Index for A: " + peakHourIndexB);
             activityPeakB = activityPeak(peakHourIndexB, normalizedVectorB);
             activityPeakCluster(activityPeakB, secondUser);
             */
            user.add(tempAlias[i + 1]);


            //map.put(secondUser, normalizedVectorB);

            /*for (int j = 1; j < user.size() - 1; j++) {
//                if (styloUser.get(j).toString().contains("_")) {
//                    //System.out.println("User: " + styloUser.get(j).toString());
//                } else {      
                List temppostTime = new ArrayList();
                //String[] otherUser = new String[1];

                String otherUser = user.get(j).toString();
                //otherUser[0] = styloUser.get(j).getUser();
                //String otherUserID = otherUser[0].toString();


                //styloUser.remove(0);

                tempAlias[j + 1] = new Alias(otherUser);
                //System.out.println("Temp Alias: " + tempAlias[j + 1]);
                int tempOtherUserID = Integer.valueOf(otherUser);

                temppostTime = TimeEquality.getPostTime(tempOtherUserID);

//                System.out.println(j + "." + " *******User*******: " + tempOtherUserID);
//                System.out.println("Post Time: " + temppostTime);

                double[] tempTimeVector = Alias.getTimeVectorArray(temppostTime);

                inactivityCluster(tempTimeVector, otherUser);

                /*double[] normalizedVector = returnNormalizedTimeVector(tempTimeVector);

                 //return normalized time vector
                 tempAlias[j + 1].setTimeStamp(normalizedVector);
                 //tempAlias[j + 1].setPostTime(temppostTime);

                 //finding peak hour for userA
                 int peakHourIndex = peakHourIndex(normalizedVector);
                 activityPeak = activityPeak(peakHourIndex, normalizedVector);
                 activityPeakCluster(activityPeak, otherUser);
                 */
                /*user.remove(j);
                user.add(j, tempAlias[j + 1]);
                //map.put(userID, normalizedVector);
                // }
            }*/

            //calculateStyloTime
            totalUserList = new ArrayList();
            totalUserList.add(nullcluster);
            totalUserList.add(users0003);
            totalUserList.add(users0306);
            totalUserList.add(users0609);
            totalUserList.add(users0912);
            totalUserList.add(users1215);
            totalUserList.add(users1518);
            totalUserList.add(users1821);
            totalUserList.add(users2100);

            //int userSize = users.size();

            System.out.println("");
//            System.out.println("Size of Cluster: " + activityPeakMap.size());
//            System.out.println("First Activity Cluster");
//            System.out.println(activityPeakMap);


            /*firstPeakCluster = new ArrayList();

             /*for (List value : activityPeakMap.keySet()) {
             firstPeakCluster.add(activityPeakMap.get(value));
             }
             */
            // clustering according to the peak number of post and within
            //threshold of 0.1
          /*  System.out.println("Cluster before 1st Activity Clustering");
             for (Map.Entry<List, List> peakMapEntry : activityPeakMap.entrySet()) {
             System.out.println(peakMapEntry);
             }*/

            /*firstActivityCluster(activityPeakMap);

             totalFirstActivityPeakCluster = new ArrayList();
             totalFirstActivityPeakCluster.add(firstActivityPeak0003);
             totalFirstActivityPeakCluster.add(firstActivityPeak0306);
             totalFirstActivityPeakCluster.add(firstActivityPeak0609);
             totalFirstActivityPeakCluster.add(firstActivityPeak0912);
             totalFirstActivityPeakCluster.add(firstActivityPeak1215);
             totalFirstActivityPeakCluster.add(firstActivityPeak1518);
             totalFirstActivityPeakCluster.add(firstActivityPeak1821);
             totalFirstActivityPeakCluster.add(firstActivityPeak2100);

             int firstActivityUserSize = 0;
             for (List<Object> sublist : totalFirstActivityPeakCluster) {
             firstActivityUserSize += sublist.size();
             }
             */

            System.out.println("\nSleeping user List");

            System.out.println("Null Cluster " + nullcluster + "\n");
            System.out.println("00-03: " + users0003 + "\n");
            System.out.println("03-06: " + users0306 + "\n");
            System.out.println("06-09: " + users0609 + "\n");
            System.out.println("09-12: " + users0912 + "\n");
            System.out.println("12-15: " + users1215 + "\n");
            System.out.println("15-18: " + users1518 + "\n");
            System.out.println("18-21: " + users1821 + "\n");
            System.out.println("21-00: " + users2100);

            /*System.out.println("\nActivity user List");
             System.out.println("Number of users in first Activity Cluster : " +   + "\n");

             System.out.println("00-03: " + firstActivityPeak0003 + "\n");
             System.out.println("03-06: " + firstActivityPeak0306 + "\n");
             System.out.println("06-09: " + firstActivityPeak0609 + "\n");
             System.out.println("09-12: " + firstActivityPeak0912 + "\n");
             System.out.println("12-15: " + firstActivityPeak1215 + "\n");
             System.out.println("15-18: " + firstActivityPeak1518 + "\n");
             System.out.println("18-21: " + firstActivityPeak1821 + "\n");
             System.out.println("21-00: " + firstActivityPeak2100 + "\n");
             *
             */

            //firstActivityPeak(totalUserList, map);
            //secondActivityPeak(totalUserList, map);
            //calculateMatchedPercentage(totalFirstActivityPeakCluster, userSize);


            totalSleepingMap = calculateMatchedList(totalUserList, userSize);
            //totalActivityMap = calculateMatchedList(totalFirstActivityPeakCluster, userSize);

            /*for(Map.Entry<String, List> sleepEntry : totalSleepingMap.entrySet()){
             List users = sleepEntry.getValue();
             if(users.isEmpty()){
             sleepingFailure++;
             }else{
             sleepingSuccess++;
                      
             }
             }*/

            System.out.println("\n Total Sleeping Users: " + totalSleepingMap);
            List SleepingUsers = new ArrayList();

            if (totalSleepingMap.isEmpty()) {
                sleepingFailure++;
            } else {
                sleepingSuccess++;
                for (Map.Entry<String, List> sleepEntry : totalSleepingMap.entrySet()) {
                    List activityPeakfirstUser = new ArrayList();
                    List activityPeaksecondUser = new ArrayList();
                    List activityPeakOtherUser = new ArrayList();
                    List<Alias> allUser = new ArrayList();

                    SleepingUsers = sleepEntry.getValue();
                    TimeClustering se = new TimeClustering(SleepingUsers);

                    allUser = se.aliases;
                    Alias[] sleepingAlias = new Alias[SleepingUsers.size() + 1];

                    String firstSleepingUser = SleepingUsers.get(0).toString();
                    dividePostTime(firstSleepingUser);
                    System.out.println("Current User: " + firstSleepingUser);

                    allUser.remove(0);

                    sleepingAlias[0] = new Alias(firstUser);
                    sleepingAlias[0].setPostTime(firstUserPostTime);
                    sleepingAlias[0].setPosts(firstUserPost);

                    double[] TimeVectorfirstUser = Alias.getTimeVectorArray(firstUserPostTime);
                    double[] normalizedVectorfirstUser = returnNormalizedTimeVector(TimeVectorfirstUser);

                    int peakHourIndexfirstUser = peakHourIndex(normalizedVectorfirstUser);
                    activityPeakfirstUser = activityPeak(peakHourIndexfirstUser, normalizedVectorfirstUser);
                    activityPeakCluster(activityPeakfirstUser, firstUser);
                    allUser.add(0, sleepingAlias[0]);

                    sleepingAlias[1] = new Alias(secondUser);
                    sleepingAlias[1].setPostTime(secondUserPostTime);
                    sleepingAlias[1].setPosts(secondUserPost);
                    double[] TimeVectorsecondUser = Alias.getTimeVectorArray(secondUserPostTime);
                    double[] normalizedVectorsecondUser = returnNormalizedTimeVector(TimeVectorsecondUser);

                    int peakHourIndexsecondUser = peakHourIndex(normalizedVectorsecondUser);
                    activityPeaksecondUser = activityPeak(peakHourIndexsecondUser, normalizedVectorsecondUser);
                    activityPeakCluster(activityPeaksecondUser, secondUser);
                    allUser.add(1, sleepingAlias[1]);

                    /*for (int x = 2; x < allUser.size(); x++) {
                        String[] otherUsers = new String[1];
                        otherUsers[0] = allUser.get(x).getUser();
                        String otherSleepingUser = otherUsers[0].toString();

                        sleepingAlias[x] = new Alias(otherSleepingUser);
                        int tempOtherUserID = Integer.valueOf(otherSleepingUser);
                        List otherUserPost = TimeEquality.getPostTime(tempOtherUserID);

                        sleepingAlias[x].setPostTime(otherUserPost);
                        sleepingAlias[x].setPosts(otherUserPost);
                        double[] TimeVectorOtherUser = Alias.getTimeVectorArray(otherUserPost);
                        double[] normalizedVectorOtherUser = returnNormalizedTimeVector(TimeVectorOtherUser);

                        int peakHourIndexOtherUser = peakHourIndex(normalizedVectorOtherUser);
                        activityPeakOtherUser = activityPeak(peakHourIndexOtherUser, normalizedVectorOtherUser);
                        activityPeakCluster(activityPeakOtherUser, otherSleepingUser);
                        allUser.remove(x);
                        allUser.add(x, sleepingAlias[x]);
                    }*/

//                    List firstPeakCluster = new ArrayList();
//
//                    for (List value : activityPeakMap.keySet()) {
//                        firstPeakCluster.add(activityPeakMap.get(value));
//                    }

                    firstActivityCluster(activityPeakMap);

                    totalFirstActivityPeakCluster = new ArrayList();
                    totalFirstActivityPeakCluster.add(firstActivityPeak0003);
                    totalFirstActivityPeakCluster.add(firstActivityPeak0306);
                    totalFirstActivityPeakCluster.add(firstActivityPeak0609);
                    totalFirstActivityPeakCluster.add(firstActivityPeak0912);
                    totalFirstActivityPeakCluster.add(firstActivityPeak1215);
                    totalFirstActivityPeakCluster.add(firstActivityPeak1518);
                    totalFirstActivityPeakCluster.add(firstActivityPeak1821);
                    totalFirstActivityPeakCluster.add(firstActivityPeak2100);

//                    int firstActivityUserSize = 0;
//                    for (List<Object> sublist : totalFirstActivityPeakCluster) {
//                        firstActivityUserSize += sublist.size();
//                    }
                    HashMap<String, List> totalActivityMap = calculateMatchedList(totalFirstActivityPeakCluster, userSize);
                    clearActivityCluster();
                    System.out.println("Total Activity Users: " + totalActivityMap);
                    
                    List activeUserList = new ArrayList();
                    for(Map.Entry<String, List> activeUserMap : totalActivityMap.entrySet()){
                         //activeUserCluster = activeUserMap.getKey();
                        activeUserList = activeUserMap.getValue();
                    }
                    
                    if (totalActivityMap.isEmpty()) {
                        activeFailure++;
                    } else {
                        activeSuccess++;
                        activeSuccessUser.add(activeUserList);
                    }  
                }
            }
            System.out.println("");
            // System.out.println("MatchedUser: " + matchedUser);
            /*System.out.println("Total Activity Users: " + totalActivityMap);
             if (totalActivityMap.isEmpty()) {
             activeFailure++;
             } else {
             activeSuccess++;
             }
             *
             */

            // double counter = 0.000;

            /*for (Map.Entry<String, List> sleepEntry : totalSleepingMap.entrySet()) {
             String SleepingCluster = sleepEntry.getKey();
             List SleepingUsers = sleepEntry.getValue();

             for (Map.Entry<String, List> activeEntry : totalActivityMap.entrySet()) {
             List tempList = new ArrayList();
             if (sleepEntry != null && activeEntry != null) {
             String ActiveCluster = activeEntry.getKey();
             List ActiveUsers = activeEntry.getValue();

             //System.out.println("Sleeping Cluster: " + SleepingCluster);
             //System.out.println("Sleeping Users: " + SleepingUsers);

             for (int d = 0; d < SleepingUsers.size(); d++) {
             if (tempList.size() != ActiveUsers.size()) {
             if (ActiveUsers.contains(SleepingUsers.get(d))) {
             Integer tempUser1 = Integer.parseInt(SleepingUsers.get(d).toString());
             tempList.add(tempUser1);
             matrixMap.put(SleepingCluster.concat(ActiveCluster), tempList);
             }
             } else {
             break;
             }
             }
             }
             }
             }
             System.out.println("\nActivity Cluster: ");
             for (Map.Entry<String, List> MatrixEntry : matrixMap.entrySet()) {
             System.out.println(MatrixEntry);
             }
             System.out.println("**************************************");

             if (!matrixMap.isEmpty()) {
             //testCluster.calculateTimeStyloEquality(matrixMap);
             success++;
             } else {
             failure++;
             }
             */
            /**
             * Finding the matched user percentage
             */
            /* HashSet hs = new HashSet();
             for (Map.Entry<String, List> MatrixEntry : matrixMap.entrySet()) {
             List values = MatrixEntry.getValue();
             hs.addAll(values);
             values.clear();
             values.addAll(hs);
             //System.out.println(MatrixEntry);
             }

             double matchedUsersSize = (double) hs.size();
             System.out.println("\nMatched User Size: " + matchedUsersSize);
             System.out.println("\nMost Matched Percentage: " + (matchedUsersSize * 100) / userSize);
             */
            clearList();

            user.remove(0);
            user.remove(userSize - 1);
            String previousUser = returnUser(firstUser);
            //List previousUserPostTime = Alias.getAllPostTime(Integer.valueOf(previousUser));
            previousAlias[0] = new Alias(previousUser);
            //previousAlias[0].setPostTime(previousUserPostTime);
            user.add(i, previousAlias[0]);
            //System.out.println("Stylouser size: " + styloUser);
        }
    }

    private static void clearActivityCluster() {

        if (!activityPeakMap.isEmpty()) {
            activityPeakMap.clear();
        }
        if (!totalFirstActivityPeakCluster.isEmpty()) {
            totalFirstActivityPeakCluster.clear();
        }
        if (!firstActivityPeak0003.isEmpty()) {
            firstActivityPeak0003.clear();
        }
        if (!firstActivityPeak0306.isEmpty()) {
            firstActivityPeak0306.clear();
        }
        if (!firstActivityPeak0609.isEmpty()) {
            firstActivityPeak0609.clear();
        }
        if (!firstActivityPeak0912.isEmpty()) {
            firstActivityPeak0912.clear();
        }
        if (!firstActivityPeak1215.isEmpty()) {
            firstActivityPeak1215.clear();
        }
        if (!firstActivityPeak1518.isEmpty()) {
            firstActivityPeak1518.clear();
        }
        if (!firstActivityPeak1821.isEmpty()) {
            firstActivityPeak1821.clear();
        }
        if (!firstActivityPeak2100.isEmpty()) {
            firstActivityPeak2100.clear();
        }
    }

    public static void clearList() {

        /*if (!totalUserList.isEmpty()) {
         totalUserList.clear();
         }*/
        if (!nullcluster.isEmpty()) {
            nullcluster.clear();
        }
        if (!users0003.isEmpty()) {
            users0003.clear();
        }
        if (!users0306.isEmpty()) {
            users0306.clear();
        }
        if (!users0609.isEmpty()) {
            users0609.clear();
        }
        if (!users0912.isEmpty()) {
            users0912.clear();
        }
        if (!users1215.isEmpty()) {
            users1215.clear();
        }
        if (!users1518.isEmpty()) {
            users1518.clear();
        }
        if (!users1821.isEmpty()) {
            users1821.clear();
        }
        if (!users2100.isEmpty()) {
            users2100.clear();
        }
        /*if (!totalSleepingMap.isEmpty()) {
         totalSleepingMap.clear();
         }
         /* if (!matchedUserMap.isEmpty()) {
         matchedUserMap.clear();
         }
         if (!matchedUser.isEmpty()) {
         matchedUser.clear();
         }
         /*
        
         /*

         if (!totalActivityMap.isEmpty()) {
         totalActivityMap.clear();
         }
         if (!matrixMap.isEmpty()) {
         matrixMap.clear();
         }
         if (!firstPeakCluster.isEmpty()) {
         firstPeakCluster.clear();
         }
         if (!activityPeakMap.isEmpty()) {
         activityPeakMap.clear();
         }*/
    }

    /**
     * creating clusters of users having same activity peak time cluster
     *
     * @param user
     * @param activityPeak
     */
    public static void activityPeakCluster(List activityPeak, String activityUser) {
        List userList = new ArrayList();
        userList.add(activityUser);
        if (activityPeakMap.containsKey(activityPeak)) {
            List temp = activityPeakMap.get(activityPeak);
            temp.add(activityUser);
            activityPeakMap.put(activityPeak, temp);
        } else {
            activityPeakMap.put(activityPeak, userList);
        }
    }

    /**
     * return normalized time vector with respect to total number of post
     *
     * @param timeVector
     * @return
     */
    public static double[] returnNormalizedTimeVector(double[] timeVector) {
        double sumB = 0;

        for (int index = 0; index < timeVector.length; index++) {
            sumB = sumB + timeVector[index];
        }

        for (int index = 0; index < timeVector.length; index++) {
            timeVector[index] = timeVector[index] / sumB;
        }
        return timeVector;
    }

    /**
     * returns index of maximum number of posts done
     *
     * @param hour
     * @return
     */
    public static int peakHourIndex(double[] hour) {
        int maxHourIndex = 0;
        double maxHour = 0.0;
        for (int i = 0; i < hour.length; i++) {
            if (hour[i] > maxHour) {
                maxHour = hour[i];
                maxHourIndex = i;
            }
        }
        return maxHourIndex;
    }

    /**
     * makes a cluster wrt to maximum index hour
     *
     * @param peakHourIndex
     * @param normalizedVector
     * @return
     */
    public static List activityPeak(int peakHourIndex, double[] normalizedVector) {
        List activityPeak = new ArrayList();
        activityPeak.add(peakHourIndex);
        double thresholdPercent = (30 * normalizedVector[peakHourIndex]) / 100;

        for (int x = peakHourIndex + 1; x < normalizedVector.length; x++) {
            if (normalizedVector[x] >= thresholdPercent) {
                activityPeak.add(x);
            } else {
                break;
            }
        }
        for (int y = peakHourIndex - 1; y >= 0; y--) {
            if (normalizedVector[y] >= thresholdPercent) {
                activityPeak.add(0, y);
            } else {
                break;
            }
        }

        return activityPeak;
    }

    /**
     * creating cluster according to their inactivity in discussion board
     *
     * @param time
     * @param user
     */
    public static void inactivityCluster(double[] time, String user) {

        double[] post_count = time;
        boolean contains = false;

        if (post_count[0] + post_count[1] + post_count[2] == 0) {
            users0003.add(user);
            contains = true;
        }
        if (post_count[3] + post_count[4] + post_count[5] == 0) {
            users0306.add(user);
            contains = true;
        }
        if (post_count[6] + post_count[7] + post_count[8] == 0) {
            users0609.add(user);
            contains = true;
        }
        if (post_count[9] + post_count[10] + post_count[11] == 0) {
            users0912.add(user);
            contains = true;
        }
        if (post_count[12] + post_count[13] + post_count[14] == 0) {
            users1215.add(user);
            contains = true;
        }
        if (post_count[15] + post_count[16] + post_count[17] == 0) {
            users1518.add(user);
            contains = true;
        }
        if (post_count[18] + post_count[19] + post_count[20] == 0) {
            users1821.add(user);
            contains = true;
        }
        if (post_count[21] + post_count[22] + post_count[23] == 0) {
            users2100.add(user);
            contains = true;
        }
        if (contains == false) {
            nullcluster.add(user);
        }
    }

    /**
     * Splitting posts between users where message posted on odd day of month
     * goes to UserA and even days message to UserB
     *
     * @param args
     * @throws SQLException
     */
    public static void dividePostTime(String ID) throws SQLException {
        firstUser = ID + "_A";
        secondUser = ID + "_B";
        int tempUserID = Integer.parseInt(ID);
        List userPosts = TimeEquality.getAllPost(tempUserID);
        List userPostsTime = TimeEquality.getPostTime(tempUserID);
        firstUserPostTime = new ArrayList();
        secondUserPostTime = new ArrayList();
        firstUserPost = new ArrayList();
        secondUserPost = new ArrayList();

        for (int i = 0; i < userPostsTime.size(); i++) {
            String date = userPostsTime.get(i).toString();
            int day = Integer.parseInt(date.substring(8, 10));
            /**
             * The odd days post are assigned to first split user and even days
             * posts are assigned to second split user.
             */
            // System.out.println(day % 2);
            if (day % 2 != 0) {
                firstUserPostTime.add(userPostsTime.get(i));
                firstUserPost.add(userPosts.get(i));
            } else {
                secondUserPostTime.add(userPostsTime.get(i));
                secondUserPost.add(userPosts.get(i));
            }
        }

//        System.out.println("FirstUserPost: " + firstUserPostTime);
//        System.out.println("\nSecondUserPost: " + secondUserPostTime);
    }

    /**
     * returns the list having maximum numbers of users from the list of lists
     *
     * @param clusters
     * @return
     */
    public static List returnMaxCluster(List<List> clusters) {

        Iterator<List> itr = clusters.iterator();
        int max = -1;
        int index = -1;
        int counter = 0;

        while (itr.hasNext()) {
            List secondList = itr.next();
            if (secondList.size() > max) {
                max = secondList.size();
                index = counter;
            }
            counter++;
        }

        return clusters.get(index);
    }

    public static void firstActivityCluster(HashMap<List, List> activityPeakMap) {

        for (Map.Entry<List, List> entry : activityPeakMap.entrySet()) {
            List key = entry.getKey();
            List value = entry.getValue();

            int checkSet03 = 0;
            int checkSet06 = 0;
            int checkSet09 = 0;
            int checkSet12 = 0;
            int checkSet15 = 0;
            int checkSet18 = 0;
            int checkSet21 = 0;
            int checkSet00 = 0;

            int offset = 3;
            for (int i = 0; i < key.size(); i++) {
                int tempHour = Integer.parseInt(key.get(i).toString());
                int bucket = tempHour / offset;
                switch (bucket) {
                    case (0):
                        checkSet03 = 1;
                        break;
                    case (1):
                        checkSet06 = 1;
                        break;
                    case (2):
                        checkSet09 = 1;
                        break;
                    case (3):
                        checkSet12 = 1;
                        break;
                    case (4):
                        checkSet15 = 1;
                        break;
                    case (5):
                        checkSet18 = 1;
                        break;
                    case (6):
                        checkSet21 = 1;
                        break;
                    case (7):
                        checkSet00 = 1;
                        break;
                }
            }
            if (checkSet03 == 1) {
                firstActivityPeak0003.addAll(value);
            }
            if (checkSet06 == 1) {
                firstActivityPeak0306.addAll(value);
            }
            if (checkSet09 == 1) {
                firstActivityPeak0609.addAll(value);
            }
            if (checkSet12 == 1) {
                firstActivityPeak0912.addAll(value);
            }
            if (checkSet15 == 1) {
                firstActivityPeak1215.addAll(value);
            }
            if (checkSet18 == 1) {
                firstActivityPeak1518.addAll(value);
            }
            if (checkSet21 == 1) {
                firstActivityPeak1821.addAll(value);
            }
            if (checkSet00 == 1) {
                firstActivityPeak2100.addAll(value);
            }
        }
    }
}
